var campanyinfo = "";
var page = 1; //默认页数
var pageSize = 10; //每页条数
var total = 0; //总条数
//分页方法
function paging(page, pageSize, total) {
	page = Number(page);

	//最大页数
	var maxPage = Math.ceil(total / pageSize) || 1;
	if(page > maxPage) {
		//取值不能超过maxPage
		page = Math.min(page, maxPage);
		tiaozhuan(param, page, pageSize, campanyinfo);
	} else if(page < 1) {
		//取值不能小于1
		page = Math.max(page, 1);
		tiaozhuan(param, page, pageSize, campanyinfo);
	} else {
		//	当前页数
		$('.ui-paging-count').val(page);
		//首页的置灰处理
		$('.js-page-first').removeClass('ui-pager-disabled');
		//上一页
		$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page", page - 1);
		//下一页
		$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page", page + 1);
		//最末页
		$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page", maxPage);
		//总页数显示
		$('.js-page-total').text(maxPage);

		//当前显示多少页的处理
		$(".ui-select-pagesize").val(pageSize);
		//第一页的处理
		if(page == 1) {
			$(".js-page-first").addClass('ui-pager-disabled').attr("data-page", "");
			$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page", "");
		}
		if(page == maxPage) {
			//最后一页的处理
			$(".js-page-last").addClass('ui-pager-disabled').attr("data-page", "");
			$(".js-page-next").addClass('ui-pager-disabled').attr("data-page", "");
		}
	}

};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change", function() {
	pageSize = $(this).val();
	tiaozhuan(param, page, pageSize, campanyinfo);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur", function() {
	page = $(this).val();
	tiaozhuan(param, page, pageSize, campanyinfo);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click", function() {
	page = $(this).attr("data-page");
	tiaozhuan(param, page, pageSize, campanyinfo);
});

//角色名查找
$('.clickFunc').on("click", function() {
	campanyinfo = $('.search-input').val();
	tiaozhuan(param, page, pageSize, campanyinfo);
});

function tiaozhuan(param, page, pageSize, campanyinfo) {

	var html = "";
	method = "post";
	datas = { page: page, pageSize: pageSize, campanyinfo: campanyinfo };
	getAgentMsg(param, method, datas);
	//	console.log(dataResult);
	for(var i = 0; i < dataResult.data.rows.length; i++) {
		var isOpen = "";
		if(dataResult.data.rows[i].state == 1) {
			isOpen = "open";
		}
		html += "<tr>";
		html += "<td><div class='switch " + isOpen + "' data-status='" + dataResult.data.rows[i].state + "'></div></td>";
		html += "<td>" + dataResult.data.rows[i].id + "</td>";
		html += "<td>" + dataResult.data.rows[i].userName + "</td>";
		html += "<td>" + dataResult.data.rows[i].adComapyName + "</td>";
		html += "<td>" + dataResult.data.rows[i].balance + "</td>";
		html += "<td><a target='_blank' href='javascript:void(0)' onclick='goAccount(" + dataResult.data.rows[i].id + ")'>进入账户</a></td>";
		html += "</tr>";
	}

	$(".roleList").html(html);
	//之后处理分页
	total = dataResult.data.total;
	paging(page, pageSize, total);
	$(".switch ").on("click", function() {
		var userStatus = $(this).attr("data-status");
		if(userStatus == 1) {
			userStatus = 0;
		} else {
			userStatus = 1;
		}
		$(this).attr("data-status", userStatus);
		$(this).toggleClass("open");
		//执行修改状态的ajax
		var param2 = "/sys/updateStatus";
		var method2 = "post";
		var userId = $(this).parent().next().text();

		var datas2 = { userId: userId, userStatus: userStatus };
		getAgentMsg2(param2, method2, datas2);
		//		console.log(dataResult2);

	});

}

function goAccount(id) {
	var param4 = "/agency/manage/namepwd?aduserid=" + id;
	$.ajax({
		url: url + param4,
		type: "post",
		dataType: 'json',
	
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result) {
				window.open("../../public/login.html?username="+result[0].userName+"&password="+result[0].password);
//				console.log(result[0].userName);
//				console.log(result[0].password);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});

}

function getAgentMsg4(param4, method4, datas4) {

}

//初始化加载数据
window.onload = function() {
	param = "/agency/manage/list";
	tiaozhuan(param, page, pageSize, campanyinfo);
}